import java.util.HashSet;
//力扣128. 最长连续序列
class Solution {
    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }

        int maxLen = 0;
        for (int num : set) {
            if (!set.contains(num - 1)) { // 只从序列起点开始找
                int currentNum = num;
                int len = 1;

                while (set.contains(currentNum + 1)) {
                    currentNum++;
                    len++;
                }

                maxLen = Math.max(maxLen, len);
            }
        }
        return maxLen;
    }
}
